<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <ul>
            <li v-for="(item, index) in goods">
                <input v-model="item.flag" type="checkbox">
                商品Id: {{ item.id }} --
                商品名称: {{ item.name }} --
                商品价格: {{ item.price.toFixed(2) }}
                商品数量:
                <button @click="goodsDel(item, index)">-</button>
                {{ item.count}}
                <button @click="goodsAdd(item)">+</button>
                商品小计: {{ item.allPrice.toFixed(2) }}
            </li>
        </ul>

        <p>
            总数量: {{ allCount }}
        </p>
        <p>
            总价格: {{ allPrice }}
        </p>
    </div>

    <script src="./vue.js"></script>

    <script>
         new Vue({
            // 元素
            el: "#app",
            // 定义的数据(状态)
            data: {
                goods: [
                    {
                        id: 11,
                        name: "华为",
                        price: 100,
                        count: 1,
                        allPrice: 100,
                        flag: true
                    },
                    {
                        id: 22,
                        name: "小米",
                        price: 200,
                        count: 1,
                        allPrice: 200,
                        flag: true
                    },
                    {
                        id: 33,
                        name: "苹果",
                        price: 300,
                        count: 1,
                        allPrice: 300,
                        flag: true
                    }
                ]
            },
            computed: {
                allCount() {
                    return this.goods.reduce(( prev , next ) => {
                        if ( next.flag ){
                            return prev + next.count
                        } else {
                            return prev
                        }
                    } , 0 )
                },
                allPrice() {
                    return this.goods.reduce (( prev , next ) => {
                        if ( next.flag ){
                            return prev + next.count * next.price
                        } else {
                            return prev
                        }
                    } , 0 )
                }
            },
            methods : {
                goodsAdd( item ){
                    item.count += 1
                    item.allPrice = item.count * item.price
                },
                goodsDel( item , index ){
                    if( item.count > 1 ){
                        item.count -= 1
                    } else {
                        this.goods.splice (index , 1 )
                    }
                    item.allPrice = item.count * item.price
                }
            }
        })
    </script>
</body>
</html>